//
//  OAAddressBookVC.m
//  OA项目
//
//  Created by innofive on 2018/4/18.
//  Copyright © 2018年 inno. All rights reserved.
//

#import "OAAddressBookVC.h"
#import "MainTabBarViewController.h"
#import "MCSwitchTabControl.h"
#import "UIBarButtonItem+Extension.h"
#import "QKSearchBar.h"
#import "OAAddressBookHeaderView.h"
#import "OASubscribelistVC.h"
#import "OASearchBuMenVC.h"
#import "OAAddPeopleVC.h"
@interface OAAddressBooklistVC()<UISearchBarDelegate>
{
    NSMutableArray *indexs;         //索引数组
    
    NSMutableArray *_titleArray;    //表中内容
}
@property (nonatomic, strong) UITextField * textF;
@property (nonatomic, strong) QKSearchBar * searchBar;
@end
@implementation OAAddressBooklistVC

- (void)viewDidLoad {
    [super viewDidLoad];

    _titleArray = [NSMutableArray array];
    
    indexs = [NSMutableArray array];
    
    for(char c = 'A'; c <= 'Z'; c++ )
    {
        [indexs addObject:[NSString stringWithFormat:@"%c",c]];
        [_titleArray addObject:[NSString stringWithFormat:@"%c",c]];
    }
}


-(void)onLoadViewByWillAppear:(UIView *)view
{
    
    QKSearchBar *searchBar = [[QKSearchBar alloc]init];
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    [view addV:searchBar];
    
    [self.tableView config:^(UITableView *tableView) {
        [view addV:tableView];
    }];
    
    
    WSELF(weakSelf);
    [self onMainQueueAddSub:^(UIView *view, CGPoint origin, CGSize size, CGPoint center) {
    
        searchBar.frame = CGRectMake(0, 0, size.width,kFitH(45));
        weakSelf.tableView.frame = CGRectMake(0,kFitH(45), size.width, size.height-kFitH(45)-49);
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _titleArray.count+1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(MCCellInfo *)tableView:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath IsHeight:(BOOL)isheight
{
    return [MCCellInfo configForCellId:cell Class:[OAAddressBookCell class] Height:kFitH(60) TableView:tableView CellStyle:(UITableViewCellStyleValue1)];
}

-(MCHFViewInfo *)tableView:(UITableView *)tableView HeaderInSection:(NSInteger)section isBiao:(BOOL)isBiao
{
    if (section==0) {
        return nil;
    }
    return [MCHFViewInfo configForViewId:header Class:[OAAddressBookHeaderView class] Height:kFitH(30) TableView:tableView];
}

//返回索引数组
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    
    return indexs;
}

////响应点击索引时的委托方法
//-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
//
//    NSInteger count = 0;
//
//    for (NSString *character in indexs) {
//
//        if ([[character uppercaseString] hasPrefix:title]) {
//            return count;
//        }
//
//        count++;
//    }
//
//    return  0;
//}

//返回每个索引的内容
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section!=0&&section<=indexs.count) {
        
    return [indexs objectAtIndex:section-1];
    }
    return @"";
}


-(void)onCancel:(UIButton *)button
{
    
    [_searchBar setShowsCancelButton:NO animated:YES];
    [_textF resignFirstResponder];
    
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:YES animated:YES];
    NSArray *subViews;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        subViews = [(searchBar.subviews[0]) subviews];
    }
    else {
        subViews = searchBar.subviews;
    }
    
    
    for (id view in subViews) {
        
        if (!_textF) {
            
            if ([view isKindOfClass:[UITextField class]]) {
                _textF = view;
            }
        }
        Class class = NSClassFromString(@"UINavigationButton");
        if ([view isKindOfClass:class]) {
            UIButton* cancelbutton = (UIButton* )view;
            [cancelbutton setTitle:@"取消" forState:UIControlStateNormal];
            // 修改文字颜色
            [cancelbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancelbutton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
            [cancelbutton AddTarget:self Action:@selector(onCancel:)];
        }
    }
}


#pragma mark 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        OASubscribelistVC *vc = [[OASubscribelistVC alloc]init];
        vc.title = @"订阅号";
        [self pushToVC:vc model:nil hideTabBar:YES];
    }
}

@end

@interface OAAddressBooklist2VC()<UISearchBarDelegate>
@property (nonatomic, strong) UITextField * textF;
@property (nonatomic, strong) QKSearchBar * searchBar;
@end
@implementation OAAddressBooklist2VC

- (void)viewDidLoad {
    [super viewDidLoad];
    

}


-(void)onLoadViewByWillAppear:(UIView *)view
{
    
    QKSearchBar *searchBar = [[QKSearchBar alloc]init];
    searchBar.placeholder = @"搜索";
    searchBar.delegate = self;
    [view addV:searchBar];
    
    [self.tableView config:^(UITableView *tableView) {
        [view addV:tableView];
    }];
    
    
    WSELF(weakSelf);
    [self onMainQueueAddSub:^(UIView *view, CGPoint origin, CGSize size, CGPoint center) {
        
        searchBar.frame = CGRectMake(0, 0, size.width,kFitH(45));
        weakSelf.tableView.frame = CGRectMake(0,kFitH(45), size.width, size.height-kFitH(45)-49);
    }];
    
}

#pragma mark 点击方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section==0) {
        OASubscribelistVC *vc = [[OASubscribelistVC alloc]init];
        vc.title = @"订阅号";
        [self pushToVC:vc model:nil hideTabBar:YES];
    }else
    {
        OASearchBuMenVC *vc = [[OASearchBuMenVC alloc]init];
        vc.title = @"市场部门";
        [self pushToVC:vc model:nil hideTabBar:YES];
        
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 1;
    }
    return 10;
}

-(MCCellInfo *)tableView:(UITableView *)tableView cellForIndexPath:(NSIndexPath *)indexPath IsHeight:(BOOL)isheight
{
    return [MCCellInfo configForCellId:cell Class:[OAAddressBook2Cell class] Height:kFitH(60) TableView:tableView];
}

-(MCHFViewInfo *)tableView:(UITableView *)tableView HeaderInSection:(NSInteger)section isBiao:(BOOL)isBiao
{
    if (section==0) {
        return nil;
    }
   return [MCHFViewInfo configForViewId:header Class:[BaseTableViewHeaderFooterView class] Height:kFitH(10) TableView:tableView];
}

-(void)onCancel:(UIButton *)button
{
    
    [_searchBar setShowsCancelButton:NO animated:YES];
    [_textF resignFirstResponder];
    
}
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [searchBar setShowsCancelButton:YES animated:YES];
    NSArray *subViews;
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        subViews = [(searchBar.subviews[0]) subviews];
    }
    else {
        subViews = searchBar.subviews;
    }
    
    
    for (id view in subViews) {
        
        if (!_textF) {
            
            if ([view isKindOfClass:[UITextField class]]) {
                _textF = view;
            }
        }
        Class class = NSClassFromString(@"UINavigationButton");
        if ([view isKindOfClass:class]) {
            UIButton* cancelbutton = (UIButton* )view;
            [cancelbutton setTitle:@"取消" forState:UIControlStateNormal];
            // 修改文字颜色
            [cancelbutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [cancelbutton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
            [cancelbutton AddTarget:self Action:@selector(onCancel:)];
        }
    }
}


@end

@interface OAAddressBookVC ()<panGestureRecognizerDelegate>
@property(nonatomic,strong)MainTabBarViewController *tabBarC;
@property(nonatomic,strong)MCSwitchTabControl *tabControl;
@property(nonatomic,strong)UIViewController *currentVC;

@end

@implementation OAAddressBookVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UIBarButtonItem *rightItem =[UIBarButtonItem OnCreateBarBtnItemImage:@"addIcon" BarBtnstyle:(RightBarBtnItemStyle) Btntag:10 target:self action:@selector(onRightButton:)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    
    MainTabBarViewController *tabBarC = [MainTabBarViewController new];
    tabBarC.panGestureDelegate = self;
    tabBarC.tabBar.hidden = YES;
    [self addChildViewController:tabBarC];
    [self.view addV:tabBarC.view];
    _tabBarC = tabBarC;
    
    
    OAAddressBooklistVC  *vc  = [[OAAddressBooklistVC alloc]init];
    vc.view.backgroundColor = [UIColor yellowColor];
    OAAddressBooklist2VC *vc1 = [[OAAddressBooklist2VC alloc]init];
    vc1.view.backgroundColor = [UIColor redColor];
    NSArray *vcs = @[vc,vc1];
    tabBarC.viewControllers = vcs;
    _currentVC =vc;
    
    
}

-(void)onLoadViewByWillAppear:(UIView *)view
{
    MCSwitchTabControl *v = [MCSwitchTabControl new];
    [v addTarget:self action:@selector(onClickChangeBtn:) forControlEvents:UIControlEventValueChanged];
    v.frame = CGRectMake(0, 0,kFitW(220),kFitH(45));
//    v.backgroundColor= [UIColor redColor];
    [v insertSegmentWithTitle:@"员工" image:nil];
    [v insertSegmentWithTitle:@"部门" image:nil];
    self.navigationItem.titleView = v;
    _tabControl = v;
    WSELF(weakSelf);
    [self onMainQueueAddSub:^(UIView *view, CGPoint origin, CGSize size, CGPoint center) {
        weakSelf.tabBarC.view.frame = CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height);
    }];
}


-(void)onRightButton:(UIButton *)button
{
    if (self.tabControl.selectedSegmentIndex==0) {
        OAAddPeopleVC *vc = [[OAAddPeopleVC alloc]init];
        vc.title = @"添加成员";
        [self pushToVC:vc model:nil hideTabBar:YES];
    }
    else
    {
        
    }
    
    
}




-(void)onClickChangeBtn:(MCSwitchTabControl *)button
{
//    [self onChangeVCForIndex:button.selectedSegmentIndex];
    self.tabBarC.selectedIndex =button.selectedSegmentIndex;
    [button segmentLineScrollToIndex:button.selectedSegmentIndex animate:YES];
}

-(void)onChangeVCForIndex:(NSInteger)index
{
    UIViewController *changeVC = self.tabBarC.viewControllers[index];
    WSELF(weakSelf);
    [self.tabBarC transitionFromViewController:_currentVC
                      toViewController:changeVC
                              duration:0
                               options:UIViewAnimationOptionTransitionNone
                            animations:^{
                                // NOPS;
                            }
                            completion:^(BOOL finished){
                                
                                weakSelf.currentVC = changeVC;
                                weakSelf.tabBarC.selectedIndex = index;
                            }
     ];
}


-(void)tabBarViewController:(MainTabBarViewController *)tabBarViewController SelectedIndex:(NSInteger)selectedIndex
{
    _tabControl.selectedSegmentIndex =selectedIndex;
    [_tabControl segmentLineScrollToIndex:selectedIndex animate:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
